﻿namespace Digitalmes.Application.Sys.Users.Queries;

/// <summary>
/// 通过用户Id查询用户信息，其中会返回结果会包含用户与角色映射关系。
/// </summary>
/// <param name="UserId"></param>
public record GetUserByIdQuery(long UserId) : IRequest<IResult<SysUserDto?>>;

internal sealed class GetUserByIdQueryHandler(IRepository<SysUser> repo) : IRequestHandler<GetUserByIdQuery, IResult<SysUserDto?>>
{
    public async Task<IResult<SysUserDto?>> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
    {
        var user = await repo.AsQueryable()
            .Includes(s => s.UserRoles)
            .FirstOrDefaultAsync(s => s.Id == request.UserId, cancellationToken);
        return Result.From(user.Adapt<SysUserDto?>());
    }
}
